package com.king.modules.monitor.controller;

import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.king.common.annotation.Log;
import com.king.common.enums.BusinessType;
import com.king.common.utils.poi.ExcelUtil;
import com.king.framework.base.controller.YySuperController;
import com.king.framework.base.domain.vo.AjaxResult;
import com.king.framework.security.service.SysPasswordService;
import com.king.modules.monitor.domain.SysLoginLogEntity;
import com.king.modules.monitor.service.SysLoginLogService;

/**
 * 系统访问记录
 * 
 * @author king
 */
@RestController
@RequestMapping("/monitor/loginlog")
public class SysLoginLogController extends YySuperController<SysLoginLogEntity> {
	@Autowired
	private SysLoginLogService loginlogService;

	@Autowired
	private SysPasswordService passwordService;

	@Log(title = "登录日志", businessType = BusinessType.EXPORT)
	@PreAuthorize("@sps.hasAuthority('monitor:loginlog:export')")
	@PostMapping("/export")
	public void export(HttpServletResponse response, SysLoginLogEntity loginlog) {
		List<SysLoginLogEntity> list = loginlogService.selectLoginlogList(loginlog);
		ExcelUtil<SysLoginLogEntity> util = new ExcelUtil<SysLoginLogEntity>(SysLoginLogEntity.class);
		util.exportExcel(response, list, "登录日志");
	}

	@PreAuthorize("@sps.hasAuthority('monitor:loginlog:del')")
	@Log(title = "登录日志", businessType = BusinessType.DELETE)
	@DeleteMapping("/{infoIds}")
	public AjaxResult remove(@PathVariable Long[] infoIds) {
		return toAjax(loginlogService.deleteLoginlogByIds(infoIds));
	}

	@PreAuthorize("@sps.hasAuthority('monitor:loginlog:del')")
	@Log(title = "登录日志", businessType = BusinessType.CLEAN)
	@DeleteMapping("/clean")
	public AjaxResult clean() {
		loginlogService.cleanLoginlog();
		return success();
	}

	@PreAuthorize("@sps.hasAuthority('monitor:loginlog:unlock')")
	@Log(title = "账户解锁", businessType = BusinessType.OTHER)
	@GetMapping("/unlock/{userName}")
	public AjaxResult unlock(@PathVariable("userName") String userName) {
		passwordService.clearLoginRecordCache(userName);
		return success();
	}
}
